<?php

namespace app\admin\service\statistics\promotion\total;

use app\admin\service\statistics\promotion\AbstractComputeStatistician;
use app\admin\service\statistics\traits\CompletionTrait;
use function show_percentage;

/**
 * 渠道数据 总计 发起支付率
 * Class LoginRegisterRate
 * @package app\admin\service\statistics\promotion\total
 */
class PayRate extends AbstractComputeStatistician
{
    // 补齐
    use CompletionTrait;

    // new_member_count,captcha_count
    protected function compute(int $index)
    {
        $statisticians = $this->statisticians;

        $pay_success_count = $statisticians['total-pay_success_count'][$index]['value'] ?? null;

        $h5liuzhi_count = $statisticians['h5-liuzhi_count'][$index]['value'] ?? null;

        $wxliuzi_count = $statisticians['wx-liuzi_count'][$index]['value'] ?? null;

        if (!is_numeric($h5liuzhi_count)) {
            $h5liuzhi_count = 0;
        }
        if (!is_numeric($wxliuzi_count)) {
            $wxliuzi_count = 0;
        }
        $liuzhi_count = $h5liuzhi_count + $wxliuzi_count;

        if (!is_numeric($pay_success_count) || !is_numeric($liuzhi_count)) {
            return '';
        }

        return show_percentage($pay_success_count, $liuzhi_count, 1);
    }

    protected function getDefaultValue()
    {
        return '0%';
    }
}